package com.jlastu.dlxy_androidapp.view.main;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.MessageAdapter;
import com.jlastu.dlxy_androidapp.entity.Message;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends AppCompatActivity {

    private ListView listViewMessages;
    private EditText editTextMessage;
    private Button buttonSend;
    private MessageAdapter messageAdapter;
    private List<Message> messagesList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_activity);

        // 初始化视图
        listViewMessages = findViewById(R.id.list_chat_messages);
        editTextMessage = findViewById(R.id.edit_message);
        buttonSend = findViewById(R.id.btn_send);

        // 初始化消息列表和适配器
        messagesList = new ArrayList<>();
        messageAdapter = new MessageAdapter(this, messagesList);
        listViewMessages.setAdapter(messageAdapter);

        // 模拟一些初始数据
        messagesList.add(new Message("顾客，对本次商品满意不", false));


        // 发送按钮点击事件处理
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
            }
        });
    }

    // 发送消息的方法
    private void sendMessage() {
        String message = editTextMessage.getText().toString().trim();
        if (!message.isEmpty()) {
            messagesList.add(new Message(message, true));
            messageAdapter.notifyDataSetChanged();
            listViewMessages.smoothScrollToPosition(messagesList.size() - 1);
            editTextMessage.setText("");
        }
    }
}